package net.wanderinghorse.json;

import java.io.IOException;
import java.io.InputStream;

/**
 * A collection of utility functions for working with the JSON API.
 */
public class JSONUtil {

    /**
     * Equivalent to (new JSONObject(readAll(is)).
     *
     * @param is Input stream to read JSON from.
     * @return a JSONObject holding the parsed data.
     * @throws IOException If the input API throws.
     * @throws JSONException If the underlying JSONObject API throws. That is, if the input
     * stream contains non-JSON data or is otherwise malformed.
     */
    public static JSONObject readAllAsJSON( final InputStream is ) throws IOException, JSONException {
           return new JSONObject( readAll( is ) );
    }

    /**
     * Reads the entire contents of the given input stream and returns it as a string.
     * This function does not do any sort of parsing of the data.
     *
     * @param is The input stream to consume.
     * @return The entire contents of the stream.
     * @throws IOException If the reading API throws.
     */
    public static String readAll( final InputStream is ) throws IOException {
        if( null == is ) {
            throw new IllegalArgumentException(JSONUtil.class.getName()+".readAll() was passed a null stream!");
        }
        StringBuilder sb = new StringBuilder();
        {
            /**
             WTF does the StringBuilder API not support byte
             or byte[] args? That lack makes populating it from an
             input stream a pain in the butt (and inefficient - with
             byte[] we could buffer a number of bytes per read()).
             */
            int rc = 0;
            while( (rc = is.read()) >= 0 )
            {
                sb.append( (char) rc );
            }
        }
        return sb.toString();
    }
}
